package com.luxury.fragment.party_fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.luxury.HomeWebActivity;
import com.luxury.ProductActivity;
import com.luxury.R;
import com.luxury.adpter.KuDaShiRecyclerAdapter;
import com.luxury.bean.KuDaShiBean;
import com.luxury.r_innerface.InterfaceLuxury;
import com.luxury.utils.URLContants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

import static com.luxury.adpter.HomeRecyclerAdapter1.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class KuDaShiFragment extends Fragment {
    private String KudashiUrl = "http://android.secoo.com/cooMaster.html?pageid=1089";
    private WebView Web_Kudashi;
/*
    private Context mContext = null;
    private List<KuDaShiBean.TopickusBean> mList = new ArrayList<>();

    @BindView(R.id.recyclerView_kuDaShi)
    RecyclerView recyclerViewKuDaShi;*/

    public KuDaShiFragment() {
        // Required empty public constructor
    }

    public static KuDaShiFragment newInstance() {
        Bundle args = new Bundle();
        KuDaShiFragment fragment = new KuDaShiFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_global_h5, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();

    }

    private void initData() {
        Web_Kudashi.loadUrl(KudashiUrl);
        Web_Kudashi.getSettings().setJavaScriptEnabled(true);
        Web_Kudashi.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                geturl(url);
                return true;
            }
        });
    }

    private void geturl(String url) {
        //判断字符串类型 拼接网址
        int start = url.indexOf(":");
        String urltype = url.substring(0, start);

        Log.i(TAG, "geturl: ++++++++++++" + start);
        int begin = url.indexOf("?") + 1;
        int end = url.indexOf("&");
        String productId = url.substring(begin, end);
        String urlString = "http://android.secoo.com/appservice/query_goods.action?" + productId;
        Log.i(TAG, "shouldOverrideUrlLoading:--------------------------- " + url);


        if (urltype.equals("http")) {
            Intent intent = new Intent(getActivity(), HomeWebActivity.class);
            intent.putExtra("url", url);
            startActivity(intent);
        } else if (urltype.equals("secoo")) {
            Log.i(TAG, "商品详情json地址" + urlString);
            Intent intent = new Intent(getActivity(), ProductActivity.class);
            intent.putExtra("url", urlString);
            intent.putExtra("productId", productId);
            startActivity(intent);
        }


    }

    private void initView(View view) {
        Web_Kudashi = (WebView) view.findViewById(R.id.webView_globalFragment_h5);

    }
/*
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_kudashi, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadNetData();
    }

    *//**
     * 加载网络数据
     *//*
    private void loadNetData() {
        //利用retrofit请求网络数据
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URLContants.BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        InterfaceLuxury interfaceLuxury = retrofit.create(InterfaceLuxury.class);
        //获取库大师json字符串数据
        Call<String> kuDaShiString = interfaceLuxury.getKuDaShiString();
        kuDaShiString.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String str = response.body();
                int begin = str.indexOf("{");
                int end = str.lastIndexOf(")");
                String jsonString = str.substring(begin,end);
                KuDaShiBean kuDaShi = parseJsonToBean(jsonString);
                mList = kuDaShi.getTopickus();
                initData();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    *//**
     * 初始化recyclerview数据
     *//*
    private void initData() {
        KuDaShiRecyclerAdapter adapter = new KuDaShiRecyclerAdapter(mContext, mList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager
                .VERTICAL, false);
        recyclerViewKuDaShi.setLayoutManager(layoutManager);
        recyclerViewKuDaShi.setAdapter(adapter);
    }
    //gson解析
    public KuDaShiBean parseJsonToBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<KuDaShiBean>() {
        }.getType());
    }*/
}
